import Vue from "vue";
import Router from "vue-router";

Vue.use(Router);

/* Layout */
import Layout from "@/layout";

export const constantRoutes = [
  {
    path: "/redirect",
    component: Layout,
    hidden: true,
    children: [
      {
        path: "/redirect/:path(.*)",
        component: () => import("@/views/redirect/index")
      }
    ]
  },
  {
    path: "/login",
    component: () => import("@/views/login/index"),
    hidden: true
  },
  {
    path: "/auth-redirect",
    component: () => import("@/views/login/auth-redirect"),
    hidden: true
  },
  {
    path: "/404",
    component: () => import("@/views/error-page/404"),
    hidden: true
  },
  {
    path: "/401",
    component: () => import("@/views/error-page/401"),
    hidden: true
  },
  {
    path: "/",
    component: Layout,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        component: () => import("@/views/dashboard/index"),
        name: "Dashboard",
        meta: { title: "dashboard", icon: "dashboard", affix: true }
      }
    ]
  }
];

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  {
    path: "/shop",
    component: Layout,
    meta: { title: "商品", icon: "el-icon-s-cooperation", noCache: true },
    children: [
      {
        path: "children/index",
        component: () => import("@/views/shop/commodityManagement/index"),
        name: "CommodityManagement",
        meta: {
          title: "商品管理",
          // icon: "el-icon-s-cooperation",
          noCache: true
        }
      },
      {
        path: "indexx",
        component: () => import("@/views/shop/commodityManagement/add/indexx"),
        name: "indexx",
        hidden: true,
        meta: {
          title: "添加商品",
          roles: ["admin"]
        }
      },
      {
        path: "classify",
        component: () => import("@/views/shop/classify"),
        name: "Classify",
        meta: { title: "商品分类" }
      },
      {
        path: "specification",
        component: () => import("@/views/shop/specification"),
        name: "Specification",
        meta: { title: "商品规格" }
      },
      {
        path: "evaluate",
        component: () => import("@/views/shop/evaluate"),
        name: "Evaluate",
        meta: { title: "商品评价" }
      }
    ]
  },
  {
    path: "/marketing",
    component: Layout,
    name: "Marketing",
    meta: { title: "营销", icon: "el-icon-platform-eleme", noCache: true },
    children: [
      {
        path: "management",
        component: () => import("@/views/marketing/management"),
        name: "Management",
        meta: {
          title: "秒杀管理"
        },
        children: [
          {
            path: "allocation",
            component: () => import("@/views/marketing/management/allocation"),
            name: "Allocation",
            meta: {
              title: "秒杀配置"
            }
          },
          {
            path: "commodity",
            component: () => import("@/views/marketing/management/commodity"),
            name: "commodity",
            meta: {
              title: "秒杀商品"
            }
          },
          // 添加商品按钮
          {
            path: "addShop",
            component: () => import("@/views/marketing/management/addShop"),
            name: "AddShop",
            hidden: true,
            meta: {
              title: "添加秒杀商品"
            }
          }
        ]
      },
      {
        path: "bargain",
        component: () => import("@/views/marketing/bargain"),
        name: "Bargain",
        meta: {
          title: "砍价管理"
        },
        children: [
          {
            path: "commodity",
            component: () => import("@/views/marketing/bargain/commodity"),
            name: "Commodity",
            meta: {
              title: "砍价商品"
            }
          },
          {
            path: "list",
            component: () => import("@/views/marketing/bargain/list"),
            name: "List",
            meta: {
              title: "砍价列表"
            }
          }
        ]
      },
      {
        path: "live",
        component: () => import("@/views/marketing/live"),
        name: "Live",
        meta: {
          title: "直播管理"
        },
        children: [
          {
            path: "list",
            component: () => import("@/views/marketing/live/list"),
            name: "List",
            meta: {
              title: "直播列表"
            }
          },
          {
            path: "found",
            component: () => import("@/views/marketing/live/foundLive"),
            name: "Found",
            meta: {
              title: "创建直播"
            }
          }
        ]
      },
      {
        path: "group",
        component: () => import("@/views/marketing/group"),
        name: "Group",
        meta: {
          title: "拼团管理"
        },
        children: [
          {
            path: "commodity",
            component: () => import("@/views/marketing/group/commodity"),
            name: "Commodity",
            meta: {
              title: "拼团商品"
            }
          },
          {
            path: "list",
            component: () => import("@/views/marketing/group/list"),
            name: "List",
            meta: {
              title: "拼团列表"
            }
          }
        ]
      }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: "*", redirect: "/404", hidden: true }
];

const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes
  });

const router = createRouter();

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}

export default router;
